import baseApi from './baseApi';
import { getMerchantId } from './merchantService';

// 响应接口类型
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T | null;
}

// 分页参数
export interface PaginationParams {
  page?: number;
  pageSize?: number;
}

// 分页结果
export interface PaginationResult {
  pagination: {
    total: number;
    page: number;
    pageSize: number;
  };
}

// 区域类型定义
export interface Area {
  id: number;
  name: string;
  status: string;
  createdAt: string;
  updatedAt: string;
}

// 区域列表请求参数
export interface AreaListParams extends PaginationParams {
  status?: string;
  search?: string;
}

// 区域列表响应
export interface AreaListResponse extends PaginationResult {
  list: Area[];
}

// 区域创建/更新参数
export interface AreaCreateParams {
  name: string;
  status?: string;
}

/**
 * 获取区域列表
 */
export const getAreaList = async (params: AreaListParams): Promise<ApiResponse<AreaListResponse>> => {
  const merchantId = getMerchantId();
  if (!merchantId) {
    return Promise.reject(new Error('未选择商户'));
  }

  try {
    const response = await baseApi.get(`/admin/restaurant/${merchantId}/areas`, { params });
    return response.data;
  } catch {
    return {
      code: -1,
      message: '获取区域列表失败',
      data: null
    };
  }
};

/**
 * 获取单个区域详情
 */
export const getAreaDetail = async (areaId: number): Promise<ApiResponse<Area>> => {
  const merchantId = getMerchantId();
  if (!merchantId) {
    return Promise.reject(new Error('未选择商户'));
  }

  try {
    const response = await baseApi.get(`/admin/restaurant/${merchantId}/areas/${areaId}`);
    return response.data;
  } catch {
    return {
      code: -1,
      message: '获取区域详情失败',
      data: null
    };
  }
};

/**
 * 创建区域
 */
export const createArea = async (params: AreaCreateParams): Promise<ApiResponse<Area>> => {
  const merchantId = getMerchantId();
  if (!merchantId) {
    return Promise.reject(new Error('未选择商户'));
  }

  try {
    const response = await baseApi.post(`/admin/restaurant/${merchantId}/areas`, params);
    return response.data;
  } catch {
    return {
      code: -1,
      message: '创建区域失败',
      data: null
    };
  }
};

/**
 * 更新区域
 */
export const updateArea = async (areaId: number, params: AreaCreateParams): Promise<ApiResponse<Area>> => {
  const merchantId = getMerchantId();
  if (!merchantId) {
    return Promise.reject(new Error('未选择商户'));
  }

  try {
    const response = await baseApi.put(`/admin/restaurant/areas/${areaId}`, params);
    return response.data;
  } catch {
    return {
      code: -1,
      message: '更新区域失败',
      data: null
    };
  }
};

/**
 * 删除区域
 */
export const deleteArea = async (areaId: number): Promise<ApiResponse<null>> => {
  const merchantId = getMerchantId();
  if (!merchantId) {
    return Promise.reject(new Error('未选择商户'));
  }

  try {
    const response = await baseApi.delete(`/admin/restaurant/areas/${areaId}`);
    return response.data;
  } catch {
    return {
      code: -1,
      message: '删除区域失败',
      data: null
    };
  }
};

const areasService = {
  getAreaList,
  getAreaDetail,
  createArea,
  updateArea,
  deleteArea
};

export default areasService; 